package com.jztd.platfrom.base.context;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

/**
 * Jackson上下文工具类
 *
 * @author lj
 * @date 2025/08/03
 */
@Slf4j
@Component
public class JacksonContextHolder implements ApplicationContextAware {
    /**
     * 全局对象映射处理实例
     */
    private static ObjectMapper OBJECT_MAPPER;

    /**
     * 获取应用上下文中配置的ObjectMapper
     *
     * @return ObjectMapper对象
     */
    public static ObjectMapper getObjectMapper() {
        return Objects.requireNonNull(OBJECT_MAPPER, "Object mapper has not been initialized");
    }

    /**
     * 对象序列化
     *
     * @param object 目标对象实例
     * @return JSON字符串
     */
    public static String serialize(Object object) {
        try {
            return object == null ? null : getObjectMapper().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象反序列化
     *
     * @param json JSON字符串
     * @param type 类型对象
     * @param <T>  对象类型
     * @return 对象实例
     */
    public static <T> T deserialize(String json, Class<T> type) {
        try {
            return json == null ? null : getObjectMapper().readValue(json, type);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象反序列化
     *
     * @param json JSON字符串
     * @param type 类型引用对象
     * @param <T>  对象类型
     * @return 对象实例
     */
    public static <T> T deserialize(String json, TypeReference<T> type) {
        try {
            return json == null ? null : getObjectMapper().readValue(json, type);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象反序列化
     *
     * @param bytes JSON字节数组
     * @param type  类型对象
     * @param <T>   对象类型
     * @return 对象实例
     */
    public static <T> T deserialize(byte[] bytes, Class<T> type) {
        try {
            return bytes == null ? null : getObjectMapper().readValue(bytes, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象反序列化
     *
     * @param bytes JSON字节数组
     * @param type  类型引用对象
     * @param <T>   对象类型
     * @return 对象实例
     */
    public static <T> T deserialize(byte[] bytes, TypeReference<T> type) {
        try {
            return bytes == null ? null : getObjectMapper().readValue(bytes, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象反序列化
     *
     * @param o
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T deserialize(Object o, Class<T> type) {
        try {
            return o == null ? null : getObjectMapper().convertValue(o, type);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param o
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T deserialize(Object o, TypeReference<T> type) {
        try {
            return o == null ? null : getObjectMapper().convertValue(o, type);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        OBJECT_MAPPER = applicationContext.getBean(ObjectMapper.class);
    }
}
